package com.linktrip.devicemanager.util;

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.linktrip.devicemanager.entity.DeviceLog;
import com.linktrip.devicemanager.websocket.entity.DataStruct;

public class DataAnalysis {
	public static final boolean DEBUG = true;
	Logger log = LoggerFactory.getLogger(DataAnalysis.class);

	@SuppressWarnings("deprecation")
	public DeviceLog analysis(byte[] temp) {
		log.debug("analysis begin...");
		DeviceLog data = new DeviceLog();

		int propVersion = 0xff & temp[1] + 0xff & temp[0];
		data.setProto_version(propVersion);

		data.setType(0xff & temp[2]);

		data.setMessage_id((short)(0xff & temp[3] + 0xff & temp[4]));

		data.setSos_flag(0xff & temp[5]);

		String deviceId = checkLen(Integer.toHexString(0xff & temp[6])) + checkLen(Integer.toHexString(0xff & temp[7]))
				+ checkLen(Integer.toHexString(0xff & temp[8])) + checkLen(Integer.toHexString(0xff & temp[9]))
				+ checkLen(Integer.toHexString(0xff & temp[10])) + checkLen(Integer.toHexString(0xff & temp[11]))
				+ checkLen(Integer.toHexString(0xff & temp[12])) + checkLen(Integer.toHexString(0xff & temp[13]));
		data.setDevice_no(deviceId);

		data.setSys_version(0xff & temp[14]);

		int mode = 0;
		if ((0xff & temp[15]) == 1) {
			mode = 1;//����ģʽ
		} else if ((0xff & temp[15]) == 2) {
			mode = 2;//ά��ģʽ
		} else if((0xff & temp[15]) == 0x81){
			mode = 3;//�쳣�ƶ�
		} else if((0xff & temp[15]) == 0x82){
			mode = 4;//�����쳣
		}
		data.setMode(mode);

		data.setLock_status(0xff & temp[16]);

		data.setBattery_stat(0xff & temp[17]);

		int charge = ((temp[18] & 0xff) == 0x11 || (temp[18] & 0xff) == 0x21) ? 1 : 2;
		data.setCharge(charge);

		data.setBlink_status(0xff & temp[19]);

		data.setBeep_count(0xff & temp[20]);

		data.setSignature(0xff & temp[21]);

		double latitude = Double.longBitsToDouble(Long.valueOf(checkLen(Integer.toHexString(0xff & temp[29]))
				+ checkLen(Integer.toHexString(0xff & temp[28])) + checkLen(Integer.toHexString(0xff & temp[27]))
				+ checkLen(Integer.toHexString(0xff & temp[26])) + checkLen(Integer.toHexString(0xff & temp[25]))
				+ checkLen(Integer.toHexString(0xff & temp[24])) + checkLen(Integer.toHexString(0xff & temp[23]))
				+ checkLen(Integer.toHexString(0xff & temp[22])), 16));
		data.setLatitude(latitude);

		double longitude = Double.longBitsToDouble(Long.valueOf(checkLen(Integer.toHexString(0xff & temp[37]))
				+ checkLen(Integer.toHexString(0xff & temp[36])) + checkLen(Integer.toHexString(0xff & temp[35]))
				+ checkLen(Integer.toHexString(0xff & temp[34])) + checkLen(Integer.toHexString(0xff & temp[33]))
				+ checkLen(Integer.toHexString(0xff & temp[32])) + checkLen(Integer.toHexString(0xff & temp[31]))
				+ checkLen(Integer.toHexString(0xff & temp[30])), 16));
		data.setLongitude(longitude);

		//int msec = Integer.parseInt(Integer.toHexString(0xff & temp[45]), 16);
		int sec = Integer.parseInt(Integer.toHexString(0xff & temp[44]), 16);
		int min = Integer.parseInt(Integer.toHexString(0xff & temp[43]), 16);
		int hours = Integer.parseInt(Integer.toHexString(0xff & temp[42]), 16);
		int date = Integer.parseInt(Integer.toHexString(0xff & temp[41]), 16);
		int month = Integer.parseInt(Integer.toHexString(0xff & temp[40]), 16);
		int year = Integer.parseInt(Integer.toHexString(0xff & temp[39]) + Integer.toHexString(0xff & temp[38]), 16);
		data.setTime_stamp(new Date(year, month, date, hours, min, sec).getTime());

		data.setHdware_version(0xff & temp[46]);

		data.setTemperature(0xff & temp[47]);

		data.setVbus(0xff & temp[48]);

		data.setIcharge(0xff & temp[49]);

		data.setVbattery(0xff & temp[50]);

		data.setNa1(0xff & temp[51]);

		data.setNa2(0xff & temp[52]);

		data.setCrc(0xff & temp[53]);

		log.debug("analysis end...");
		log.warn("[to]:{}",toString(data));
		return data;
	}
	
	@SuppressWarnings("deprecation")
	public DataStruct analysisAll(byte[] temp) {
		log.debug("analysis begin...");
		DataStruct data = new DataStruct();

		int propVersion = 0xff & temp[1] + 0xff & temp[0];
		data.setProtoVersion(propVersion);

		data.setType(temp[2]);

		data.setMessageId(ByteUtil.byte2Short(new byte[]{temp[4],temp[3]}));

		data.setSosFlag(temp[5]);

		String deviceId = checkLen(Integer.toHexString(0xff & temp[6])) + checkLen(Integer.toHexString(0xff & temp[7]))
				+ checkLen(Integer.toHexString(0xff & temp[8])) + checkLen(Integer.toHexString(0xff & temp[9]))
				+ checkLen(Integer.toHexString(0xff & temp[10])) + checkLen(Integer.toHexString(0xff & temp[11]))
				+ checkLen(Integer.toHexString(0xff & temp[12])) + checkLen(Integer.toHexString(0xff & temp[13]));
		data.setDeviceId(deviceId);

		data.setSysVersion(0xff & temp[14]);

		int mode = 0;
		if ((0xff & temp[15]) == 1) {
			mode = 1;//����ģʽ
		} else if ((0xff & temp[15]) == 2) {
			mode = 2;//ά��ģʽ
		} else if((0xff & temp[15]) == 0x81){
			mode = 3;//�쳣�ƶ�
		} else if((0xff & temp[15]) == 0x82){
			mode = 4;//�����쳣
		}
		data.setMode(mode);

		data.setLockStatus(0xff & temp[16]);

		data.setBatteryStat(0xff & temp[17]);

		int charge = ((temp[18] & 0xff) == 0x11 || (temp[18] & 0xff) == 0x21) ? 1 : 2;
		data.setCharge(charge);

		data.setBlinkStatus(temp[19]);

		data.setBeepCount(temp[20]);

		data.setSignature(0xff & temp[21]);

		double latitude = Double.longBitsToDouble(Long.valueOf(checkLen(Integer.toHexString(0xff & temp[29]))
				+ checkLen(Integer.toHexString(0xff & temp[28])) + checkLen(Integer.toHexString(0xff & temp[27]))
				+ checkLen(Integer.toHexString(0xff & temp[26])) + checkLen(Integer.toHexString(0xff & temp[25]))
				+ checkLen(Integer.toHexString(0xff & temp[24])) + checkLen(Integer.toHexString(0xff & temp[23]))
				+ checkLen(Integer.toHexString(0xff & temp[22])), 16));
		data.setLatitude(latitude);

		double longitude = Double.longBitsToDouble(Long.valueOf(checkLen(Integer.toHexString(0xff & temp[37]))
				+ checkLen(Integer.toHexString(0xff & temp[36])) + checkLen(Integer.toHexString(0xff & temp[35]))
				+ checkLen(Integer.toHexString(0xff & temp[34])) + checkLen(Integer.toHexString(0xff & temp[33]))
				+ checkLen(Integer.toHexString(0xff & temp[32])) + checkLen(Integer.toHexString(0xff & temp[31]))
				+ checkLen(Integer.toHexString(0xff & temp[30])), 16));
		data.setLongitude(longitude);

		//int msec = Integer.parseInt(Integer.toHexString(0xff & temp[45]), 16);
		int sec = Integer.parseInt(Integer.toHexString(0xff & temp[44]), 16);
		int min = Integer.parseInt(Integer.toHexString(0xff & temp[43]), 16);
		int hours = Integer.parseInt(Integer.toHexString(0xff & temp[42]), 16);
		int date = Integer.parseInt(Integer.toHexString(0xff & temp[41]), 16);
		int month = Integer.parseInt(Integer.toHexString(0xff & temp[40]), 16);
		int year = Integer.parseInt(Integer.toHexString(0xff & temp[39]) + Integer.toHexString(0xff & temp[38]), 16);
		data.setTimeStamp(new Date(year, month, date, hours, min, sec).getTime());

		data.setHdwareVer(0xff & temp[46]);

		data.setTemp(temp[47]);

		data.setvBus(0xff & temp[48]);

		data.setiCharge(0xff & temp[49]);

		data.setvBattery(0xff & temp[50]);

		data.setNa1(0xff & temp[51]);

		data.setNa2(0xff & temp[52]);

		data.setCrc(0xff & temp[53]);
		if(temp.length > 54){
			short segmentCount = Short.valueOf(checkLen(Integer.toHexString(0xff & temp[55])) +checkLen(Integer.toHexString(0xff & temp[54])),16);
			data.setSegmentCount(segmentCount);
			short segmentId = Short.valueOf(checkLen(Integer.toHexString(0xff & temp[57])) +checkLen(Integer.toHexString(0xff & temp[56])),16);
			data.setSegmentId(segmentId);
		}
			

		log.debug("analysis end...");
		return data;
	}
	
	public DeviceLog getDeviceLog(DataStruct ds){
		DeviceLog data = new DeviceLog();
		data.setProto_version(ds.getProtoVersion());

		data.setType(ds.getType() & 0xff);

		data.setMessage_id(ds.getMessageId());

		data.setSos_flag(ds.getSosFlag() & 0xff);

		data.setDevice_no(ds.getDeviceId());

		data.setSys_version(ds.getSysVersion());

		data.setMode(ds.getMode());

		data.setLock_status(ds.getLockStatus());

		data.setBattery_stat(ds.getBatteryStat());

		data.setCharge(ds.getCharge());

		data.setBlink_status(ds.getBlinkStatus() & 0xff);

		data.setBeep_count(ds.getBeepCount());

		data.setSignature(ds.getSignature());

		data.setLatitude(ds.getLatitude());

		data.setLongitude(ds.getLongitude());
		
		data.setTime_stamp(ds.getTimeStamp());

		data.setHdware_version(ds.getHdwareVer());

		data.setTemperature(ds.getTemp());

		data.setVbus(ds.getvBus());

		data.setIcharge(ds.getiCharge());

		data.setVbattery(ds.getvBattery());

		data.setNa1(ds.getNa1());

		data.setNa2(ds.getNa2());

		data.setCrc(ds.getCrc());

		log.debug("analysis end...");
		log.warn("[to]:{}",toString(data));
		return data;
	}
	
	public String getDeviceId(byte[] temp){
		String deviceId = checkLen(Integer.toHexString(0xff & temp[6])) + checkLen(Integer.toHexString(0xff & temp[7]))
		+ checkLen(Integer.toHexString(0xff & temp[8])) + checkLen(Integer.toHexString(0xff & temp[9]))
		+ checkLen(Integer.toHexString(0xff & temp[10])) + checkLen(Integer.toHexString(0xff & temp[11]))
		+ checkLen(Integer.toHexString(0xff & temp[12])) + checkLen(Integer.toHexString(0xff & temp[13]));
		return deviceId;
	}

	public static String checkLen(String temp) {
		if (temp.length() < 2)
			return "0" + temp;

		return temp;
	}

	private String toString(DeviceLog log) {
		// TODO Auto-generated method stub
		return log.analysisToString();
	}
}
